var crypto=require("crypto");
var utils={

	random_string:function(len){
		var $chars='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';

		var maxPos=$chars.length;
		var str='';
		for (var i=0;i<len;i++){
			str+=$chars.charAt(Math.floor(Math.random()*maxPos));
		}
		return str;
	},

	random_int_str:function(len){
		var $chars='0123456789';

		var maxPos=$chars.length;
		var str='';
		for (var i=0;i<len;i++){
			str+=$chars.charAt(Math.floor(Math.random()*maxPos));
		}
		return str;
	},

	//[begin,end]
	random_int:function(begin,end){
		var num=begin+Math.random()*(end-begin+1);
		num=Math.floor(num);
		if(num>end)
			num=end;

		return num;
	},

	//当前时间戳
 	timestamp:function(){
		var date=new Date();
		var time=Date.parse(date);
		time=time/1000;
		return time;
	},

	//时间戳转日期
	timestamp2date:function(time){
		var date=new Date();
		date.setTime(time);
		return [date.getFullYear(),date.getMonth(),date.getDay(),date.getHours(),date.getMinutes(),date.getSeconds()];
	},

	//"2017-06-28 22:12:30"
	date2timestamp:function(strdate){
		var date=new Date(strdate.replace(/-/g,'/'));
		var time=Date.parse(date);
		return time/1000;
	},

	//今天的时间戳
	timestamp_today:function(){
		var date=new Date();
		date.setHours(0);
		date.setMinutes(0);
		date.setSeconds(0);

		var time=Date.parse(date);
		return time/1000;
	},

	//昨天的时间戳
	timestamp_yesterday:function(){
		var time=this.timestamp_today();
		return time-24*60*60;
	},

	//编码
	base64_encode:function(content){
		var buf=new Buffer(content);
		var base64=buf.toString("base64");
		return base64;
	},

	base64_decode:function(base64_str){
		var buf=new Buffer(base64_str,"base64");
		return buf;
	},

	md5:function(data){
		var md5=crypto.createHash("md5");
		md5.update(data);
		return md5.digest("hex");
	},

	sha1:function(data){
		var md5=crypto.createHash("sha1");
		md5.update(data);
		return md5.digest("hex");
	},
};

module.exports=utils;